//
//  FCSuitBoundTableView.m
//  webViewLocal
//
//  Created by 陈文斌 on 2021/7/28.
//

#import "FCSuitBoundTableView.h"
#import "NSObject+KVCChangeType.h"
#import "FCCGetCurrentVcTool.h"
@interface FCSuitBoundTableView()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation FCSuitBoundTableView
-(NSString *)cellLevy{
    if (!_cellLevy) {
        _cellLevy = @"deployModel";
    }
    return _cellLevy;
}
-(void)setRequestUseCell:(NSString *)requestUseCell{
    _requestUseCell = requestUseCell;
    [self registerClass:NSClassFromString(requestUseCell) forCellReuseIdentifier:requestUseCell];
}
//-(instancetype)init{
//    if (self = [super init]) {
//        self.estimatedSectionHeaderHeight=0;
//        self.estimatedSectionFooterHeight=0;
//        self.estimatedRowHeight = self.cellHeight>0 ? self.cellHeight : 44;
//        self.rowHeight =UITableViewAutomaticDimension;//自适应cell高度
//        self.delegate = self;
//        self.dataSource = self;
//        if (@available(iOS 15.0, *)) {
//               self.sectionHeaderTopPadding = 0;
//           }
//    }
//    return self;
//}
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.estimatedSectionHeaderHeight=0;
        self.estimatedSectionFooterHeight=0;
        self.estimatedRowHeight = self.cellHeight>0 ? self.cellHeight : 44;
        self.rowHeight =UITableViewAutomaticDimension;//自适应cell高度
        self.delegate = self;
        self.dataSource = self;
        if (@available(iOS 15.0, *)) {
               self.sectionHeaderTopPadding = 0;
           }
    }
    return self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.setTableViewHeadBlock) {
        return self.setTableViewHeadBlock(self,section ,nil);
    }
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.setTableViewFootBlock) {
        return self.setTableViewFootBlock(self,section ,nil);
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView =[UIView new];
    if (self.setTableViewHeadBlock) {
        self.setTableViewHeadBlock(self,section, headView);
    }
    return headView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editStyle) {
        return YES;
    }
    return NO;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editStyle) {
        return self.editStyle;
    }else if(self.setEditStyleAction){
        return self.setEditStyleAction(tableView,indexPath);
    }
    return UITableViewCellEditingStyleNone;
}
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.setTableViewAction) {
        return self.setTableViewAction(self,indexPath);
    }else{
        return nil;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView =[UIView new];
    if (self.setTableViewFootBlock) {
        self.setTableViewFootBlock(self,section, footView);
    }
    return footView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.numberOfSectionsBlock) {
        return self.numberOfSectionsBlock(self);
    }
    for (id obj in self.contentModelArr) {
        if ([obj isKindOfClass:[NSArray class]]) {
            return self.contentModelArr.count;
        }
    }
    if(self.numberSection == YES){
        return self.contentModelArr.count>0?self.contentModelArr.count:1;
    }
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.numberOfRowsInSectionBlock) {
        return self.numberOfRowsInSectionBlock(self,section);
    }
    for (id obj in self.contentModelArr) {
        if ([obj isKindOfClass:[NSArray class]]) {
            if ([self.contentModelArr indexOfObject:obj] == section) {
                return ((NSArray*)obj).count;
            }
           
        }
    }
    if(self.numberSection == YES){
        for (id obj in self.contentModelArr) {
            if ([obj isKindOfClass:[NSArray class]]) {
                return ((NSArray*)obj).count;
            }
        }
        return self.contentModelArr.count>0?1:0;
    }

    return self.contentModelArr.count;
}
-(void)setAddtargetDelegate:(BOOL)addtargetDelegate{
    _addtargetDelegate = addtargetDelegate;
    if (addtargetDelegate) {
        self.delegate = self;
        self.dataSource = self;
    }else{
        self.delegate = nil;
        self.dataSource = nil;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.cellForRowAtIndexPathBlock) {
        UITableViewCell *cell = self.cellForRowAtIndexPathBlock(self,indexPath);
        if (cell) {
            return self.cellForRowAtIndexPathBlock(self,indexPath);
        }
       
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.requestUseCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.requestUseCell];
    }
    if (self.cellLevy) {
        id model ;
        for (id obj in self.contentModelArr) {
            if ([obj isKindOfClass:[NSArray class]]) {
                model = self.contentModelArr[indexPath.section][indexPath.row];
                break;
            }
        }if (!model) {
            if (self.numberSection) {
                model = self.contentModelArr[indexPath.section];
            }else{
                model = self.contentModelArr[indexPath.row];
            }
        }
        [cell setValue:model forKey:self.cellLevy];
    }
    if ([self.tableViewDelegate respondsToSelector:@selector(gettableView:andTableViewCell:cellForRowAtIndexPath:)]) {
        return [self.tableViewDelegate gettableView:self andTableViewCell:cell cellForRowAtIndexPath:indexPath];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.tableViewDidSelect){
        self.tableViewDidSelect(self,indexPath);
        return;
    }else{
    if ([self.tableViewDelegate respondsToSelector:@selector(tableViewSelect: didSelectRowAtIndexPath:)]) {
        [self.tableViewDelegate tableViewSelect:self didSelectRowAtIndexPath:indexPath];
        return;
    }
    }
    if (self.pushController) {
        UIViewController *controller = [[self.pushController alloc] init];
        if (self.pushKeyValue) {
            if (self.cellLevy) {
                id model ;
                for (id obj in self.contentModelArr) {
                    if ([obj isKindOfClass:[NSArray class]]) {
                        model = self.contentModelArr[indexPath.section][indexPath.row];
                        break;
                    }
                }if (!model) {
                    if (self.numberSection) {
                        model = self.contentModelArr[indexPath.section];
                    }else{
                        model = self.contentModelArr[indexPath.row];
                    }
                }
            for (NSString *key in self.pushKeyValue) {
                NSString *value =self.pushKeyValue[key];
                if ([key containsString:@","]) {
                    for (NSString *subKey in [key componentsSeparatedByString:@","]) {
                        if ([model checkIsExistPropertyWithInstance:model verifyPropertyName:subKey andLogAllProperty:NO]) {
                            model = [model valueForKey:subKey];
                        }
                    }
                }else{
                    if ([model checkIsExistPropertyWithInstance:model verifyPropertyName:key andLogAllProperty:YES]) {
                        model = [model valueForKey:key];
                    }
                }
                if([controller checkIsExistPropertyWithInstance:self.pushController verifyPropertyName:value andLogAllProperty:NO]&&model){
                    [controller setValue:model forKey:value];
                }
            }
        }
    }
        [[FCCGetCurrentVcTool getCurrentViewController].navigationController pushViewController:controller animated:YES];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(self.tableViewDidScroll){
        self.tableViewDidScroll(scrollView);
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tableVieWillDisplay) {
        self.tableVieWillDisplay(tableView, cell, indexPath);
    }
}

@end
